﻿using System;
using System.Data;
using BLL;

namespace Stationery
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Application["HomNay"] = 0;
            Application["HomQua"] = 0;
            Application["TuanNay"] = 0;
            Application["TuanTruoc"] = 0;
            Application["ThangNay"] = 0;
            Application["ThangTruoc"] = 0;
            Application["TatCa"] = 0;
            Application["visitors_online"] = 0;
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //string fullOrigionalpath = Request.Url.ToString().ToUpper();

            //if (fullOrigionalpath.Contains("/LOGIN"))
            //{
            //    Context.RewritePath("/Login.aspx");
            //}
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }



        protected void Session_Start(object sender, EventArgs e)
        {
            //Session.Timeout = 150;
            //string request_cookies = Request.Headers["Cookie"];

            //if ((null != request_cookies) &&

            //        (request_cookies.IndexOf("ASP.NET_SessionId") >= 0))
            //{

            //    //cookie existed, so this new one is due to timeout.

            //    //Redirect the user to the login page

            //    System.Diagnostics.Debug.WriteLine("Session expired!");

            //    Response.Redirect(Constants.HOME_PAGE + "?" +

            //                      Constants.PARAM_REQUEST + "=" +

            //                      Constants.PARAM_REQUEST_VALUE_TIMEOUT);

            //}
            //

            try
            {
                Application.Lock();
                Application["visitors_online"] = Convert.ToInt64(Application["visitors_online"]) + 1;
                Application.UnLock();
                BLL_TB_ThongKe mThongKe = new BLL_TB_ThongKe();
                DataTable dtb = mThongKe.Get_TB_ThongKe_All();
                if (dtb.Rows.Count > 0)
                {
                    Application["HomNay"]     = long.Parse("0" + dtb.Rows[0]["HomNay"]).ToString("#,###");
                    Application["HomQua"]     = long.Parse("0" + dtb.Rows[0]["HomQua"]).ToString("#,###");
                    Application["TuanNay"]    = long.Parse("0" + dtb.Rows[0]["TuanNay"]).ToString("#,###");
                    Application["TuanTruoc"]  = long.Parse("0" + dtb.Rows[0]["TuanTruoc"]).ToString("#,###");
                    Application["ThangNay"]   = long.Parse("0" + dtb.Rows[0]["ThangNay"]).ToString("#,###");
                    Application["ThangTruoc"] = long.Parse("0" + dtb.Rows[0]["ThangTruoc"]).ToString("#,###");
                    Application["TatCa"]      = long.Parse("0" + dtb.Rows[0]["TatCa"]).ToString("#,###");
                }
                dtb.Dispose();
                mThongKe = null;
            }
            catch 
            { 
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            try
            {
                Application.Lock();
                Application["visitors_online"] = Convert.ToInt64(Application["visitors_online"]) - 1;
                Application.UnLock();
            }
            catch
            {

            }
        }
    }
}